# -*- coding:utf-8 -*-
import time
import random
import sqlite3
import requests
from collections import deque
from save_Proxies import SaveProxies


def save1_proxies(pg=1):
	"""
	调用save_Proxies.py中的SaveProxies()
	将proxy存储至数据库（已对proxy是否可用进行了测试）
	"""
	run = SaveProxies()
	urls = ['http://www.xicidaili.com/nn/{}'.format(page) for page in range(1, pg+1)]
	for url in urls:
		run.save_proxies(url)
		time.sleep(random.randint(5, 7))
	run.close()


def temp_deque(pg=1):
	"""将数据库的proxy取出存至deque"""
	proxies = deque([])  # 此双向队列，用于接收数据库已存储的proxy
	save1_proxies(pg)
	conn = sqlite3.connect('proxies.db')
	cursor = conn.cursor()
	cursor.execute("SELECT proxy FROM tb_proxies")
	proxies_data = cursor.fetchall()
	for proxy in proxies_data:
		proxies.append(proxy)
	return proxies


def temp_list(pg=1):
	"""将数据库的proxy取出存至deque"""
	proxies = []  # 此双向队列，用于接收数据库已存储的proxy
	save1_proxies(pg)
	conn = sqlite3.connect('proxies.db')
	cursor = conn.cursor()
	cursor.execute("SELECT proxy FROM tb_proxies")
	proxies_data = cursor.fetchall()
	for proxy in proxies_data:
		proxies.append(proxy)
	return proxies


def ttest_proxies(pg=1):
	"""
	对deque中的proxy进行测试
	"""
	while True:
		proxies = temp_deque(pg)
		try:
			proxy = proxies.popleft()[0]  # 从队列左侧取出（proxies在调用前进行存储）
			response = requests.get('https://www.baidu.com/', proxies={'http': 'http://' + str(proxy)})
			time.sleep(random.randint(2, 5))
			if response.status_code == 200:
				print ('proxy: {} is available'.format(proxy))
				proxies.append(proxy)  # 从队列右侧追加
			else:
				print ('proxy: {} is unavailable'.format(proxy))
				proxies.remove(proxy)  # 若测试不通过则移除
		except IndexError:
			break


if __name__ == '__main__':
	temp_list()
